package com.shell.bassadapter;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private int [] img={R.drawable.img,R.drawable.bg2,R.drawable.bg3,R.drawable.bg4,R.drawable.bg5,R.drawable.bg6};
    private String[] names={"张三","李四","王五","阿YueYue","阿悠悠","刘德华"};
    private String[] occupations={"飞行员","学生","教师","歌手","歌手","演员"};
    private ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"你选择的名字是："+names[position]+"\n职业是："+occupations[position],Toast.LENGTH_LONG).show();
            }
        });
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            convertView= LayoutInflater.from(MainActivity.this).inflate(R.layout.layout,null);
//            ImageView imageView=convertView.findViewById(R.id.iv_image);
//            TextView name=convertView.findViewById(R.id.tv_name);
//            TextView occupation=convertView.findViewById(R.id.tv_occupation);
//
//            imageView.setBackgroundResource(img[position]);
//            name.setText(names[position]);
//            occupation.setText(occupations[position]);
//            return convertView;
            ViewHolder vh = new ViewHolder();
            if(convertView==null){
                //创建View
                convertView = getLayoutInflater().inflate(R.layout.layout, null);
                vh.iv = (ImageView) convertView.findViewById(R.id.iv_image);
                vh.tv = (TextView) convertView.findViewById(R.id.tv_name);
                vh.tv2=(TextView) convertView.findViewById(R.id.tv_occupation);
                convertView.setTag(vh);
            }else{
                vh = (ViewHolder)convertView.getTag();
            }
            vh.iv.setImageResource(img[position]);
            vh.tv.setText(names[position]);
            vh.tv2.setText(occupations[position]);
            return convertView;

        }
    }
    static class ViewHolder{
        ImageView iv;
        TextView tv;
        TextView tv2;
    }
}
